package com.etc.api;

import com.etc.entity.ShopCar;
import com.etc.entity.SysUser;
import com.etc.service.ShopCarService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.function.DoubleBinaryOperator;

/**
 * @Author kalista
 * @Description 购物车控制层
 * @Date 2020/12/18  16:20
 **/
@WebServlet("/shopcarservlet")
public class ShopCarServlet  extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 1. 获取值
        Double totalprice = Double.parseDouble(req.getParameter("totalPrice"));
        Integer number = Integer.parseInt(req.getParameter("number"));
        Integer fid = Integer.parseInt(req.getParameter("fid"));
        HttpSession session = req.getSession();
        SysUser user = (SysUser) session.getAttribute("user");
        Integer uid = user.getUid();
        // 2. 保存值
        ShopCarService shopCarService = new ShopCarService();
        Integer save = shopCarService.save(new ShopCar(fid, uid, number, totalprice));
        PrintWriter writer = resp.getWriter();
        if (save > 0){
            req.getRequestDispatcher("todetail?fid="+fid).forward(req,resp);
        }else{
            writer.println("添加失败");
        }


    }
}
